// SCENARIO SCRIPT

// This is the special script for your entire scenario. It contains
// special encounters and code accessable from anywhere in the scenario. it also
// contains the code that initializes important special things in the
// scenario (like shops and names and descriptions of special items).

// You can create your own states, but you should give all of them numbers greater than
// or equal to 10.

beginscenarioscript;

variables;
short i,j,k,choice;
string dlgstr;

body;

// This is the state that is called every time the scenario is loaded,
// even when a save file in the scenario is loaded. Some things that should go here:
//    Names and descriptions of special items.
//    Names and effects of custom special abilities.
beginstate LOAD_SCEN_STATE;
	init_special_abil(0,"Set Cut Scene Speed",10);
	init_special_abil(1,"Make Potions",11);
	
	init_special_item(1,"Steel key","This is a small steel key that you found under Castle Putideum.");
	init_special_item(2,"Silver key","This is a small silver key that you found in the ashes of Lord Putidus.");
break;

// This is the state that is called only once at the very beginning of 
// the scenario. Some things that should go here:
//    The stuff in shops.
//    Creating horses and boats.
beginstate START_SCEN_STATE;
	// Initialize a few shops.

	// Shop 0 - food
	add_item_to_shop(0,4,50);
	add_item_to_shop(0,6,50);
	add_item_to_shop(0,8,50);
	
	// Shop 1 - supplies
	add_item_to_shop(1,449,50); // Battle Medicine Kit
	add_item_to_shop(1,450,50); // Pilum
	
	force_start_day(-1);
	
	// Prep the party for the scenario.
	//
	// What's going on here? Well, certain skills aren't used at all in this
	// scenario, because they don't make sense within the setting. Those skills
	// are: Bows, Priest Spells, Mage Spells, Arcane Lore, Nature Lore, and
	// Potion Making. They get converted into other, oft-neglected skills.
	//
	// Then the party gets the appropriate custom special abilities.
	j = 0;
	i = 0;
	while (i <= 3) {
		if (char_ok(i)) {
			// Remove all spells
			k = 0;
			while (k <= 19) {
				set_flag(271 + i,k,get_spell_level(i,0,k));
				if (get_spell_level(i,0,k))
					change_spell_level(i,0,k,0 - get_spell_level(i,0,k));
				k = k + 1;
				}
			k = 0;
			while (k <= 19) {
				set_flag(275 + i,k,get_spell_level(i,1,k));
				if (get_spell_level(i,1,k))
					change_spell_level(i,1,k,0 - get_spell_level(i,1,k));
				k = k + 1;
				}
			
			// change Bows to Thrown Missiles
			set_flag(260 + i,0,get_stat_levels_bought(i,6));
			alter_stat(i,7,get_stat_levels_bought(i,6)); 
			alter_stat(i,6,0 - get_stat_levels_bought(i,6));
			
			// change Priest Spells to First Aid
			set_flag(260 + i,1,get_stat_levels_bought(i,12));
			alter_stat(i,17,get_stat_levels_bought(i,12)); 
			alter_stat(i,12,0 - get_stat_levels_bought(i,12));
			
			// change Mage Spells to Thrown Missiles
			set_flag(260 + i,2,get_stat_levels_bought(i,11));
			alter_stat(i,7,get_stat_levels_bought(i,11)); 
			alter_stat(i,11,0 - get_stat_levels_bought(i,11));
			
			// change Arcane Lore to Thrown Missiles
			set_flag(260 + i,3,get_stat_levels_bought(i,13));
			alter_stat(i,7,get_stat_levels_bought(i,13)); 
			alter_stat(i,13,0 - get_stat_levels_bought(i,13));
			
			// change Nature Lore to Thrown Missiles
			set_flag(260 + i,4,get_stat_levels_bought(i,16));
			alter_stat(i,7,get_stat_levels_bought(i,16)); 
			alter_stat(i,16,0 - get_stat_levels_bought(i,16));
			
			// change Pole Weapons to Melee Weapons
			set_flag(260 + i,5,get_stat_levels_bought(i,5));
			alter_stat(i,4,get_stat_levels_bought(i,5)); 
			alter_stat(i,5,0 - get_stat_levels_bought(i,5));
			
			// change Potion Making to First Aid
			set_flag(260 + i,6,get_stat_levels_bought(i,14));
			alter_stat(i,17,get_stat_levels_bought(i,14)); 
			alter_stat(i,14,0 - get_stat_levels_bought(i,14));
			
			// change Tool Use to Dexterity
			set_flag(260 + i,7,get_stat_levels_bought(i,15));
			alter_stat(i,1,get_stat_levels_bought(i,15));
			alter_stat(i,15,0 - get_stat_levels_bought(i,15));
			
			change_custom_abil_uses(i,0,1); // give Change Cutscene Speed
			change_custom_abil_uses(i,1,1); // give Make Potions
			
			if (get_species(i) == 0) // check if human
				j = j + 1;
			}
		i = i + 1;
		}
	
	print_str("All spells removed.");
	print_str("Bows changed to Thrown Missiles.");
	print_str("Priest Spells changed to First Aid.");
	print_str("Mage Spells changed to Thrown Missiles.");
	print_str("Arcane Lore changed to Thrown Missiles.");
	print_str("Nature Lore changed to Thrown Missiles.");
	print_str("Pole Weapons changed to Melee Weapons.");
	print_str("Potion Making changed to First Aid.");
	
	i = 0;
	if (char_ok(0))
		i = i + get_level(0);
	if (char_ok(1))
		i = i + get_level(1);
	if (char_ok(2))
		i = i + get_level(2);
	if (char_ok(3))
		i = i + get_level(3);
	i = i / party_size();
	
	if ((j < party_size()) || (i > 10)) // if not all human or high level, warn
		message_dialog("You probably shouldn't bring non-humans or an experienced party into this scenario. It wouldn't make much sense. If you do bring an experienced party in, be aware that your stats are altered. They will be restored eventually.","Be sure you read the readme, if you haven't already.");
	
	set_flag(251,0,3);
	
break;

// This state is called every tick wherever the party is in the scenario.
// You can use the set_state
beginstate START_STATE;
break;

// Place your own states below. Give each a number at least 10.
beginstate 10; // Special ability to set cut scene speed
	change_custom_abil_uses(who_used_custom_abil(),0,1);
	
	reset_dialog();
	add_dialog_str(0,"How fast would you like the cut scenes to go?",0);
	add_dialog_choice(0,"Slow speed.");
	add_dialog_choice(1,"Normal speed.");
	add_dialog_choice(2,"Faster!");
	choice = run_dialog(1);
	if (choice == 1)
		set_flag(251,0,4);
	if (choice == 2)
		set_flag(251,0,3);
	if (choice != 3)
		end();
	
	reset_dialog();
	add_dialog_str(0,"How fast would you like the cut scenes to go?",0);
	add_dialog_choice(0,"High speed.");
	add_dialog_choice(1,"Very high speed.");
	choice = run_dialog(1);
	if (choice == 1)
		set_flag(251,0,2);
	if (choice == 2)
		set_flag(251,0,1);
break;

beginstate 11;
	change_custom_abil_uses(who_used_custom_abil(),1,1);
	
if (is_outdoor() == 0) {
	message_dialog("You are not in the wilderness where herbs grow in abundance, so you cannot collect any.","If you go outdoors, you should be able to find some.");
	end();
	}
	
	clear_buffer();
	append_string("You look around for herbs to make healthy medicines for yourself. ");
	
	// i keeps track of how many elixirs have been given
	// j keeps track of whether any elixirs have been given at all
	
	j = 0;
	
	// Get 5 Healing Potions.
	i = 0;
	if (has_num_of_item(445) < 5) {
		i = 1;
		while ((has_num_of_item(445) < 4) && (reward_give(445))) {
			i = i + 1;
			}
		if (i >= 1) {
			append_string("You find enough to make: ");
			append_number(i);
			append_string(" healing potion");
			if (i > 1)
				append_string("s");
			j = 1;
			}
		}
	
	// Get 3 Curing Potions.
	i = 0;
	if (has_num_of_item(446) < 3) {
		i = 1;
		while ((has_num_of_item(446) < 2) && (reward_give(446))) {
			i = i + 1;
			}
		if (i >= 1) {
			if (j == 0)
				append_string("You find enough to make: ");
			else
				append_string(", ");
			append_number(i);
			append_string(" curing potion");
			if (i > 1)
				append_string("s");
			j = 1;
			}
		}
			
	// Get 5 Strength Potions.
	i = 0;
	if (has_num_of_item(447) < 5) {
		i = 1;
		while ((has_num_of_item(447) < 4) && (reward_give(447))) {
			i = i + 1;
			}
		if (i >= 1) {
			if (j == 0)
				append_string("You find enough to make: ");
			else
				append_string(", ");
			append_number(i);
			append_string(" strength potion");
			if (i > 1)
				append_string("s");
			j = 1;
			}
		}
		
	// Get 5 Haste Potions.
	i = 0;
	if (has_num_of_item(448) < 5) {
		i = 1;
		while ((has_num_of_item(448) < 4) && (reward_give(448))) {
			i = i + 1;
			}
		if (i >= 1) {
			if (j == 0)
				append_string("You find enough to make: ");
			else
				append_string(", ");
			append_number(i);
			append_string(" haste potion");
			if (i > 1)
				append_string("s");
			j = 1;
			}
		}
		
	if (j > 0)
		append_string(".");
	else
		append_string("You find none.");
		
	get_buffer_text(dlgstr);
	message_dialog(dlgstr,"You take a moment after your search to offer the appropriate prayers to the gods or goddesses of this area so that they will not be offended by your taking plants.");
	
break;

beginstate 12;
	j = 0; // Zero the variable.
	
	if (get_stat(who_used_custom_item(),17) >= 5) {
		if (run_select_a_pc(0)) {
			if (who_used_custom_item() != get_selected_pc()) {
				if ((char_dist_to_loc(who_used_custom_item(),char_loc_x(get_selected_pc()),char_loc_y(get_selected_pc())) <= 1) || (is_combat() == 0)) {
					// Heal the character.
					i = get_health(get_selected_pc()); // Record original health
					change_char_health(get_selected_pc(),5 + get_ran(3,1,get_stat(who_used_custom_item(),17)));
					
					// Produce message to tell what happened.
					clear_buffer();
					append_char_name(get_selected_pc());
					append_string(" is healed for ");
					append_number(get_health(get_selected_pc()) - i);
					append_string(".");
					get_buffer_text(dlgstr);
					print_str(dlgstr);
					
					// Cures statuses 0 (poison), 7 (disease), 11 (paralysis), 12 (acid)
					set_char_status(get_selected_pc(),0,0 - get_char_status(get_selected_pc(),0),0,1);
					set_char_status(get_selected_pc(),7,0 - get_char_status(get_selected_pc(),7),0,1);
					set_char_status(get_selected_pc(),11,0 - get_char_status(get_selected_pc(),11),0,1);
					set_char_status(get_selected_pc(),12,0 - get_char_status(get_selected_pc(),12),0,1);
					
					j = 1; // Using the kit worked
					
					}
				else {
					clear_buffer();
					append_char_name(get_selected_pc());
					append_string(" is too far away from ");
					append_char_name(who_used_custom_item());
					append_string(" to use Battle Medicine.");
					get_buffer_text(dlgstr);
					message_dialog(dlgstr,"");
					}
				}
			else 
				message_dialog("You can't use Battle Medicine on yourself.","");
			}
		}
	else 
		message_dialog("Your First Aid skill must be at least 5 in order to use Battle Medicine.","");
	
	if (j == 0) // If using the kit failed, give back the charge that was lost.
		char_give_item(who_used_custom_item(),449);
break;

beginstate 13; // i is first character, j is whether he dies
	j = 0;
	i = 0;
	while (char_ok(i) == 0)
		{i = i + 1; }
	if (char_loc_x(i) == 20)
		if (char_loc_y(i) != 36)
			j = 1;
	if (char_loc_x(i) == 21)
		if (char_loc_y(i) != 37)
			j = 1;
	if (char_loc_x(i) == 22)
		if (char_loc_y(i) != 37)
			j = 1;
	if (char_loc_x(i) == 23)
		if (char_loc_y(i) != 38)
			j = 1;
	if (char_loc_x(i) == 24)
		if (char_loc_y(i) != 37)
			j = 1;
	if (char_loc_x(i) == 25)
		if (char_loc_y(i) != 36)
			j = 1;
	if (char_loc_x(i) == 26)
		if ((char_loc_y(i) != 36) && (char_loc_y(i) != 35))
			j = 1;
	if (char_loc_x(i) == 27)
		if (char_loc_y(i) != 35)
			j = 1;
	if (char_loc_x(i) == 28)
		if (char_loc_y(i) != 34)
			j = 1;
	if (char_loc_x(i) == 29)
		if (char_loc_y(i) != 35)
			j = 1;
	if (char_loc_x(i) == 30)
		if (char_loc_y(i) != 36)
			j = 1;
	if (char_loc_x(i) == 31)
		if (char_loc_y(i) != 36)
			j = 1;
	if (char_loc_x(i) == 32)
		if (char_loc_y(i) != 37)
			j = 1;
	if (char_loc_x(i) == 33)
		if (char_loc_y(i) != 38)
			j = 1;
	if (char_loc_x(i) == 34)
		if (char_loc_y(i) != 37)
			j = 1;
	if (char_loc_x(i) == 35)
		if (char_loc_y(i) != 38)
			j = 1;
	if (j == 1) {
		message_dialog("As he is stepping off the space he was on, the ground gives way under your leader, and he plummets to his death!","");
		kill_char(i,2,0);
		}
break;